<?php

class script_pdd extends L {

    /**
     * 描述 ：重新刊登
     * 作者 ：Zoro.Zhu
    **/
    public function aPublic(){
        $sql = "SELECT
            productId,account
        FROM
            `ali_product` 
        WHERE
            state = 45 
        AND errorData->>'$.info' = '调用过于频繁，请调整调用频率' ";
        $info = L::sql($sql);
        foreach($info as $row){
            of_base_com_mq::set(['pddProductAdd', $row['productId'], 60], ['productId' => $row['productId'], 'account' => $row['account']]);
        }
        echo 'ok!' . time();
    }

    /**
     * 描述 ：删除已下架的产品
     * 作者 ：Zoro.Zhu
     **/
    public function deleteProduct(){
        set_time_limit(0);
        $account = 'pdd57838014118';
        $token = model_vsAccount::getAccountToken(['account' => $account]);
        $data['accessToken'] = $token['accessToken'];
        $data['clientId'] = $token['appKey'];
        $data['clientSecret'] = $token['appSecret'];
        $sql = "SELECT 
            pddId 
        FROM 
            `ali_product` 
        WHERE 
            `account` = '{$account}'
        AND `state` = 60";
        $info = L::sql($sql);
        $productIds = array_column($info, 'pddId');
        $productIds = array_chunk($productIds, 50);
        foreach($productIds as $productId){
            $data['goodsId'] = $productId;
            $res = model_api_pdd::deleteCommit($data);
            p($res);
        }
    }

    /**
     * 描述 : 将产品全部下架
     * 作者 : Zoro.Zhu
     */
    public function updStatus(){
        set_time_limit(0);
        $accountAy = [
            'pdd57838014118',
            'pdd50116154684'
        ];
        foreach ($accountAy as $account){
            $token = model_vsAccount::getAccountToken(['account' => $account]);
            $data['accessToken'] = $token['accessToken'];
            $data['clientId'] = $token['appKey'];
            $data['clientSecret'] = $token['appSecret'];
            $data['isOnsale'] = 0;
            $sql = "SELECT 
                pddId 
            FROM 
                `ali_product` 
            WHERE 
                `account` = '{$account}'
            AND `state` = 50";
            $info = L::sql($sql);
            foreach($info as $row){
                $data['goodsId'] = $row['pddId'];
                $res = model_api_pdd::updateSaleStatus($data);
                if(!empty($res['error_msg']) && $res['error_msg'] == '商品不存在'){
                    L::sql("UPDATE `ali_product` 
                    SET `state` = '70' 
                    WHERE
                        `pddId` = '{$row['pddId']}'");
                }else if(!empty($res['goods_sale_status_set_response'])){
                    L::sql("UPDATE `ali_product` 
                    SET `state` = '60' 
                    WHERE
                        `pddId` = '{$row['pddId']}'");
                }
//                p($res);exit;
            }
        }
        echo 'ok';
    }

    /**
     * 描述 : 将产品数量较少的供应商的商品下架
     * 作者 : Zoro.Zhu
     */
    public function offShelf(){
        set_time_limit(0);
        $sql = "select productId,pddId,account from ali_product where manuName in (
            SELECT
                manuName
            FROM
                `ali_product` 
            WHERE
                state = 50 
                AND manuName != '' 
            GROUP BY
                manuName 
            HAVING
                count( 0 ) <= 3
        )";
        $info = L::sql($sql);
        foreach($info as $row){
            $updStatusRes = serv_rapi_pdd::updProductStatus($row['pddId'], $row['account'], 0);
            if(!empty($updStatusRes['goods_sale_status_set_response']['is_success'])){
                $errorInfo = [
                    'info' => '阿里巴巴已下架',
                    'date' => date('Y-m-d H:i:s')
                ];
                $sql = "UPDATE `ali_product` 
                    SET 
                        `errorData` = '" . addslashes(json_encode($errorInfo)) . "',
                        `state` = 60 
                    WHERE
                        `productId` = '{$row['productId']}'";
                L::sql($sql);
            }
        }
    }

    /**
     * 描述 ：测试类目预测接口
     * 作者 ：Zoro.Zhu
     **/
    public static function mappingCat(){
        $account = 'pdd57838014118';
        $token = model_vsAccount::getAccountToken(['account' => $account]);
        $data['accessToken'] = $token['accessToken'];
        $data['clientId'] = $token['appKey'];
        $data['clientSecret'] = $token['appSecret'];
        $data['catId'] = '123662002';
        $data['catName'] = '惯性/回力玩具';
        $data['productName'] = '抖音同款儿童四驱惯性越野车 仿真特技摇摆汽车玩具 地摊货源';
        $res = model_api_pdd::mappingCat($data);
        if(!empty($res)){
            return !empty($res['cat_id4']) ? $res['cat_id4'] : $res['cat_id3'];
        }
        return false;
    }

    /**
     * 描述 : 修改所有刊登状态产品的信息
     * 作者 : Zoro.Zhu
     */
    public function updProduct(){
        $sql = "SELECT 
           productId,account
        FROM 
            `ali_product` 
        WHERE state = 50";
        $info = L::sql($sql);
        foreach ($info as $row){
            of_base_com_mq::set(array('pddProductUpd', $row['productId']), array('productId' => $row['productId'], 'account' => $row['account']));
        }
        echo 'ok';
    }

}
return true;